﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static RoleController;

public class FightManager : MonoBehaviour
{

    static public FightManager instance;

    public Dictionary<string, List<RoleController>> dicFightRoles;

    private void Awake()
    {
        dicFightRoles = new Dictionary<string, List<RoleController>>();
        instance = this;
    }

    private void OnDestroy()
    {
        instance = null;
    }

    internal static string GetFightId()
    {
        return Guid.NewGuid().ToString();
    }


    public void FindEmeny(string fightId, RoleCamp camp, ref List<RoleController> cacheListRole)
    {
        cacheListRole.Clear();
        if (dicFightRoles.ContainsKey(fightId) == false)
            return;

        List<RoleController> listRoles = dicFightRoles[fightId];
        //Debug.Log(listRoles.Count);
        for (int i = 0; i < listRoles.Count; i++)
        {
            if (listRoles[i].hp <= 0)
            {
                listRoles.RemoveAt(i);
                i--;
            }
            else if (listRoles[i].camp != camp)
            {
                cacheListRole.Add(listRoles[i]);
            }
        }
    }

    internal string MergeFight(string fightId1, string fightId2)
    {
        //if (dicFightRoles.ContainsKey(fightId2) == false)
        //    dicFightRoles.Add(fightId2, new List<RoleController>());

        if (dicFightRoles.ContainsKey(fightId2))
        {
            dicFightRoles[fightId1].AddRange(dicFightRoles[fightId2]);
            dicFightRoles.Remove(fightId2);
        }
        return fightId1;
    }

    internal void AddFight(string fightId, List<RoleController> listRoels)
    {
        if (dicFightRoles.ContainsKey(fightId) == false)
            dicFightRoles.Add(fightId, new List<RoleController>());

        dicFightRoles[fightId].AddRange(listRoels);
    }

    internal void OverFight(string fightId)
    {
        if (dicFightRoles.ContainsKey(fightId) != false)
        {
            dicFightRoles.Remove(fightId);  
        }
    }
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}